package com.knowif.pojo;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.validation.constraints.NotEmpty;

import com.fasterxml.jackson.annotation.JsonFormat;
import org.springframework.validation.annotation.Validated;

import com.knowif.enumInfo.InfoStatusEnum;

@Validated
public class UserShare {
    private Integer id;

    /**
     * 分享时间
     */
    @JsonFormat(pattern="yyyy-MM-dd HH:mm:ss",timezone="GMT+8")
    private Date createTime;

    /**
     * 拥有者id
     */
    @NotEmpty(message = "拥有者id不能为空")
    private Integer ownerId;
    
    private TUser owner;

    /**
     * 分享者id
     */
    @NotEmpty(message = "分享者id不能为空")
    private Integer sharerId;

    private TUser sharer;
    
    /**
     * 红包id
     */
    @NotEmpty(message = "红包id不能为空")
    private Integer userRedpacketId;
    
    
    private UserRedpacket userRedpacket;

    /**
     * 状态
     */
    private Integer status;

    /**
     * 分享金额
     */
    private Double shareMoney;

    public Integer getId() {
        return id;
    }

    public void setId(Integer id) {
        this.id = id;
    }

    public Date getCreateTime() {
    	if(createTime == null){
    		createTime = new Date();
    	}
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Integer getOwnerId() {
        return ownerId;
    }

    public void setOwnerId(Integer ownerId) {
        this.ownerId = ownerId;
    }

    public Integer getSharerId() {
        return sharerId;
    }

    public void setSharerId(Integer sharerId) {
        this.sharerId = sharerId;
    }

    public Integer getUserRedpacketId() {
        return userRedpacketId;
    }

    public void setUserRedpacketId(Integer userRedpacketId) {
        this.userRedpacketId = userRedpacketId;
    }

    public Integer getStatus() {
    	if(status == null){
    		status = InfoStatusEnum.ISOK.getStatus();
    	}
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public Double getShareMoney() {
        return shareMoney;
    }

    public void setShareMoney(Double shareMoney) {
        this.shareMoney = shareMoney;
    }

	public TUser getOwner() {
		return owner;
	}

	public void setOwner(TUser owner) {
		this.owner = owner;
	}

	public TUser getSharer() {
		return sharer;
	}

	public void setSharer(TUser sharer) {
		this.sharer = sharer;
	}

	public UserRedpacket getUserRedpacket() {
		return userRedpacket;
	}

	public void setUserRedpacket(UserRedpacket userRedpacket) {
		this.userRedpacket = userRedpacket;
	}

	public static void saveOwner(List<UserShare> userShares, List<TUser> owners) {
		if(userShares == null || userShares.isEmpty() ||owners == null || owners.isEmpty()){
			return;
		}
		for(UserShare userShare : userShares){
			for(TUser owner : owners){
				if(userShare.getOwnerId().equals(owner.getId())){
					userShare.setOwner(owner);
				}
			}
		}
	}
	
	public static void saveShare(List<UserShare> userShares, List<TUser> sharers) {
		if(userShares == null || userShares.isEmpty() ||sharers == null || sharers.isEmpty()){
			return;
		}
		for(UserShare userShare : userShares){
			for(TUser sharer : sharers){
				if(userShare.getSharerId().equals(sharer.getId())){
					userShare.setSharer(sharer);
				}
			}
		}
	}
	
	
	
	public static Set<Integer> getOwnerIds(List<UserShare> userShares) {
		if(userShares == null || userShares.isEmpty()){
			return new HashSet<Integer>();
		}
		Set<Integer> sharerids = new HashSet<Integer>();
		for(UserShare userShare : userShares){
			sharerids.add(userShare.getOwnerId());
		}
		return sharerids;
	}
	
	
	public static Set<Integer> getShareIds(List<UserShare> userShares) {
		if(userShares == null || userShares.isEmpty()){
			return new HashSet<Integer>();
		}
		Set<Integer> sharerids = new HashSet<Integer>();
		for(UserShare userShare : userShares){
			sharerids.add(userShare.getSharerId());
		}
		return sharerids;
	}
	
	@Override
	public String toString() {
		return "UserShare [id=" + id + ", createTime=" + createTime + ", ownerId=" + ownerId + ", owner=" + owner
				+ ", sharerId=" + sharerId + ", sharer=" + sharer + ", userRedpacketId=" + userRedpacketId
				+ ", userRedpacket=" + userRedpacket + ", status=" + status + ", shareMoney=" + shareMoney + "]";
	}
}